<?php
class Model_Services extends Model_Abstract
{
            //set form variables
    protected $_name,
                $_last_name,
                $_email,
                $_phone,
                $_zip,
                $_bedrooms,
                $_bathrooms,
                $_best_time,
                $_details,
                $_periodicity,
                $_clean_type,
                $_address,

                $_residence_type,
                $_business_type,
                $_rooms,
                $_square,
                $_levels;

    public function __construct($values,$clean_type){
        parent::__construct();

        $this->_clean_type  = $clean_type;
        $this->_name        = $values['name'];
        $this->_last_name   = $values['last_name'];
        $this->_email       = $values['email'];
        $this->_phone       = Html::chars($values['phon_num']);
        $this->_zip         = $values['zip_code'];
        $this->_bedrooms    = !empty($values['how_many_bedrooms']) ? $values['how_many_bedrooms'] : '';
        $this->_bathrooms   = !empty($values['how_many_bathrooms']) ? $values['how_many_bathrooms'] : '';
        $this->_best_time   = !empty($values['best_days']) ? Html::chars($values['best_days']) : '';
        $this->_details     = !empty($values['request_details']) ? Html::chars($values['request_details']) : '';
        $this->_periodicity = !empty($values['how_often']) ? $values['how_often'] : '';
        $this->_address     = Html::chars($values['address']);

        $this->_residence_type      = $values['residence_type'];

        $this->_business_type       = !empty($values['business_type']) ? Html::chars($values['business_type']) : '';
        $this->_rooms               = !empty($values['how_many_rooms']) ? $values['how_many_rooms'] : '';
        $this->_square              = !empty($values['square']) ? $values['square'] : '';
        $this->_levels              = !empty($values['how_many_levels']) ? $values['how_many_levels'] : '';

    }
    public function sendRequestOnEmail($requestId){
        $email = $this->db->query('SELECT * FROM `settings` WHERE `key`="email"')->fetch();
        $emailVal = $email['value'];

        $title = $this->db->query('SELECT * FROM `settings` WHERE `key`="title"')->fetch();
        $titleVal = $title['value'];

        $msg = $this->createMessage($requestId);

        Mailer::sendMail($emailVal, 'New request', $msg, $titleVal.'<noreply@'.$_SERVER['HTTP_HOST'].'>');
    }

    public function createMessage($requestId){
        $msg = '';

        if($requestId){
            $msg.= 'Request Id:'.$requestId.PHP_EOL;
        }
        //clean type
        foreach(self::$CLEAN_TYPES as $clean_type=>$clean_value){
            if($clean_type === $this->_clean_type){
                $msg.= 'Clean type : '.$clean_value.PHP_EOL;
                break(1);
            }
        }
        //bedrooms
        foreach(self::$BEDROOMS as $bedroom_type=>$bedroom_val){
            if($bedroom_type ===$this->_bedrooms){
                $msg.= 'Bedrooms : '.$bedroom_val.PHP_EOL;
                break(1);
            }
        }
        //batrooms
        foreach(self::$BATHROOMS as $bathroom_type=>$bathroom_val){
            if($bathroom_type ===$this->_bathrooms){
                $msg.= 'Bathrooms : '.$bathroom_val.PHP_EOL;
                break(1);
            }
        }
        //how often
        foreach(self::$PERIDIOCITY as $often_type=>$often_val){
            if($often_type ===$this->_periodicity){
                $msg.= 'How often : '.$often_val.PHP_EOL;
                break(1);
            }
        }
        //best time
        if($this->_best_time !==''){
            $msg.= 'Best time : '.$this->_best_time.PHP_EOL;
        }
        //details
        if($this->_details !==''){
            $msg.= 'Details : '.$this->_details.PHP_EOL;
        }
        //name
        if($this->_name !==''){
            $msg.= 'Name : '.$this->_name.PHP_EOL;
        }
        //last name
        if($this->_last_name !==''){
            $msg.= 'Last name : '.$this->_last_name.PHP_EOL;
        }
        //email
        if($this->_email !==''){
            $msg.= 'Email : '.$this->_email.PHP_EOL;
        }
        //phone
        if($this->_phone !==''){
            $msg.= 'Phone : '.$this->_phone.PHP_EOL;
        }
        //zip
        if($this->_zip !==''){
            $msg.= 'Zip : '.$this->_zip.PHP_EOL;
        }
        //address
        if($this->_address !==''){
            $msg.= 'Address : '.$this->_address.PHP_EOL;
        }

        //basetype
        if($this->_residence_type !==''){
            $msg.= 'Residence type : '.$this->_residence_type.PHP_EOL;
        }

        //business type
        if($this->_business_type !==''){
            $msg.= 'Business type : '.$this->_business_type.PHP_EOL;
        }

        //rooms
        if($this->_rooms !==''){
            $msg.= 'Rooms : '.$this->_rooms.PHP_EOL;
        }

        //square
        if($this->_square !==''){
            $msg.= 'Square : '.$this->_square.PHP_EOL;
        }

        //levels
        foreach(self::$LEVELS as $level_type=>$level_val){
            if($level_type === $this->_levels){
                $msg.= 'Bathrooms : '.$level_val.PHP_EOL;
                break(1);
            }
        }

        return $msg;
    }

    public function addRequest(){
        return $this->db->query('INSERT INTO `clean_requests` (`id`,`clean_type`,`residence_type`,`name`,`last_name`,`email`,`phone`,`zip_code`,`bedrooms`,`bathrooms`,`best_time`,`details`,`periodicity`,`address`,`business_type`,`rooms`,`square`,`levels`)VALUES("","'.$this->_clean_type.'","'.$this->_residence_type.'","'.$this->_name.'","'.$this->_last_name.'","'.$this->_email.'","'.$this->_phone.'","'.$this->_zip.'","'.$this->_bedrooms.'","'.$this->_bathrooms.'","'.$this->_best_time.'","'.$this->_details.'","'.$this->_periodicity.'","'.$this->_address.'","'.$this->_business_type.'","'.$this->_rooms.'","'.$this->_square.'","'.$this->_levels.'")')->insertId();
    }
    

}